import { topCategory } from '@/api/constants.js'
import { allCategory } from '@/api/category.js'

export default {
  namespaced: true,
  state: () => {
    return {
      // 初始化
      categoryList: topCategory.map((item) => {
        return { name: item }
      }),

      activeItem: { activeChild: {} }
    }
  },
  mutations: {
    setList(state, data) {
      state.categoryList = data
    },

    setActiveItem(state, data) {
      state.activeItem = data
    },

    // 显示 / 隐藏 导航栏
    showOrhide(state, { item, flag }) {
      const category = state.categoryList.find(listItem => listItem.id === item.id)
      category.open = flag
    }
  },

  actions: {
    async getList({ commit }) {
      let { result } = await allCategory()
      result = result.map(item => {
        return {
          open: false,
          ...item
        }
      })
      commit('setList', result)
    }
  }
}
